﻿using System;
using System.Xml;
using System.Web;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Collections.Generic;
using System.Security.Permissions;

namespace Tencent.OA.App
{
    /// <summary>
    /// <para> 表示一个调度任务服务提供程序的配置节。</para>
    /// </summary>
    public class ScheduleServiceProviderElement : ConfigurationElement
    {
        private AssemblyQualifiedTypeNameConverter typeConverter = new AssemblyQualifiedTypeNameConverter();

        private const string namePropertyName = "name";
        private const string typePropertyName = "type";

        /// <summary>
        /// <para> 获取或设置调度任务服务提供程序的名称。</para>
        /// </summary>
        [ConfigurationProperty(namePropertyName, IsRequired = true, IsKey = true)]
        public string ScheduleServiceName
        {
            get { return (string)base[namePropertyName]; }
            set { base[namePropertyName] = value; }
        }

        /// <summary>
        /// <para> 获取或设置配置中的调度任务服务提供程序类型的完全限定名。</para>
        /// </summary>
        /// <value><para> 一个字符串, 调度任务服务提供程序类型的完全限定名。 </para></value>
        [ConfigurationProperty(typePropertyName, IsRequired = true)]
        public string ScheduleServiceTypeName
        {
            get { return (string)this[typePropertyName]; }
            set { this[typePropertyName] = value; }
        }

        /// <summary>
        /// <para> 获取或设置调度任务服务提供程序的类型。</para>
        /// </summary>
        /// <value>
        /// <para> 一个 <see cref="System.Type"/>，调度任务服务提供程序的类型。</para>
        /// </value>
        public Type ScheduleServiceType
        {
            get { return (Type)typeConverter.ConvertFrom(ScheduleServiceTypeName); }
            set { ScheduleServiceTypeName = typeConverter.ConvertToString(value); }
        }

        /// <summary>
        /// <para> 获取或设置这个调度任务循环执行的间隔时间(分钟)。如果没有设置这个值，则获取ScheduleServiceProviderElementCollection对象的Interval值。</para>
        /// </summary>
        [ConfigurationProperty("interval")]
        public int Interval
        {
            get { return (int)this["interval"]; }
            set { this["interval"] = value; }
        }

        /// <summary>
        /// <para> 获取或设置这个调度任务每天执行的固定时间(时:分, HH:mm)。设定此值必须将executingAbsoluteTime设定为true。</para>
        /// </summary>
        [ConfigurationProperty("executingTime", DefaultValue = "02:00:00")]
        public DateTime ExecutingTime
        {
            get { return (DateTime)this["executingTime"]; }
            set { this["executingTime"] = value; }
        }

        /// <summary>
        /// 获取或设置这个调度任务是否在每天固定的时间执行。默认为false。
        /// </summary>
        [ConfigurationProperty("executingAbsoluteTime", DefaultValue = false)]
        public bool ExecuteAbsoluteTime
        {
            get { return (bool)this["executingAbsoluteTime"]; }
            set { this["executingAbsoluteTime"] = value; }
        }

        /// <summary>
        /// <para> 获取或设置这个调度任务是否在服务启动时即刻执行一次。</para>
        /// </summary>
        [ConfigurationProperty("executeOnStart", DefaultValue = true)]
        public bool ExecuteOnStart
        {
            get { return (bool)this["executeOnStart"]; }
            set { this["executeOnStart"] = value; }
        }

        /// <summary>
        /// <para> 获取或设置这个调度任务是否可以关闭。</para>
        /// </summary>
        [ConfigurationProperty("enableShutDown", DefaultValue = false)]
        public bool EnableShutDown
        {
            get { return (bool)this["enableShutDown"]; }
            set { this["enableShutDown"] = value; }
        }

        /// <summary>
        /// <para> 此方法支持基础结构，因此不建议从代码中直接调用。</para>
        /// </summary>
        /// <param name="reader">读取配置未见的XmlReader。</param>
        public void DeserializeElement(XmlReader reader)
        {
            base.DeserializeElement(reader, false);
        }

    }

    /// <summary>
    /// <para> 表示一个调度任务服务提供程序配置的集合。</para>
    /// </summary>
    [ConfigurationCollection(typeof(ScheduleServiceProviderElement), CollectionType = ConfigurationElementCollectionType.BasicMap),
    AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
    public class ScheduleServiceProviderElementCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new ScheduleServiceProviderElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((ScheduleServiceProviderElement)element).ScheduleServiceName;
        }

        protected override string ElementName
        {
            get { return "task"; }
        }

        ///// <summary>
        ///// <para> 获取或设置可应用于所有调度任务循环执行的间隔时间(分钟)。默认为5分钟。</para>
        ///// </summary>
        //[ConfigurationProperty("interval", DefaultValue = 5)]
        //public int Interval
        //{
        //    get { return (int)this["interval"]; }
        //    set { this["interval"] = value; }
        //}
    }

}
